<?php
/**
 * Http工具类
 *
 * @author   
 * @copyright     Copyright (c) 2007-2013 bagesoft. All rights reserved.
 * @link          http://www.bagecms.com
 * @license       http://www.bagecms.com/license
 * @version       v3.1.0
 */

namespace app\libs;

use app\extensions\XUtils;
use yii;

class XHttp
{
    /**
     * Http Get Request
     * @param $urlMixed
     * @param array $data
     * @param bool $debug
     * @param array $header
     * @param int $overTime
     * @return null
     * @throws \ErrorException
     */
    public static function get($urlMixed, $data = [], $debug = false, $header = [], $overTime = 15)
    {
        $curl = new XCurl();
        if (is_array($header) && $header) {
            foreach ($header as $key => $value) {
                $curl->setHeader($key, $value);
            }
        }
        $curl->setOpt(CURLOPT_TIMEOUT, $overTime);
        $curl->get($urlMixed, $data);
        if ($debug == true) {
            //XUtils::dump($curl->request_headers);
        }

        return $curl->response;
    }

    /**
     * Http Post Request
     * @param $urlMixed
     * @param array $data
     * @param bool $debug
     * @param array $header
     * @param int $overTime
     * @return null
     */
    public static function post($urlMixed, $data = [], $debug = false, $header = [], $overTime = 15)
    {
        $curl = new XCurl();
        $curl->setOpt(CURLOPT_TIMEOUT, $overTime);
        if (is_array($header) && $header) {
            foreach ($header as $key => $value) {
                $curl->setHeader($key, $value);
            }
        }
        $curl->post($urlMixed, $data);
        if ($debug == true) {
            //XUtils::dump($curl->request_headers);
        }

        return $curl->response;
    }

    /**
     * 异步上传文件
     * @param $filename
     * @param $path
     * @param string $type
     * @param int $dfsType
     * @return mixed
     */
    public static function upload($filename, $path, $type = '.jpg', $dfsType = 7)
    {
        if (function_exists('curl_file_create')) {
            $cFile = curl_file_create($path, $type, $filename);
        } else {
            $cFile = '@' . realpath($path) . ';type=' . $type . ';filename=' . $filename;
        }
        //token，安全验证
        $dfsToken  = md5(Yii::$app->params['descKey']['dfsApiDesc']['backenKey']) . '####' . date('Y-m-d H:i:s');
        $utilities = new XUtils();
        $dfsToken  = $utilities->apiSTD3Encrypt($dfsToken, 'dfsApiDesc');
        $data      = ['file' => $cFile, 'dfsCdn' => 'cdn6', 'dfsType' => $dfsType, 'dfsToken' => $dfsToken]; //文件路径，前面要加@，表明是文件上传.

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, Yii::$app->params['fastdfsUrl']);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); //二进制流的方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // curl_getinfo($ch);
        $return_data = curl_exec($ch);
        curl_close($ch);
        return $return_data;
    }
}
